<?php 
	session_start();
	include('function/connection.php');
	include('function/function.php');
	
	if(isset($_GET['action']) && $_GET['action'] == "insert"){
		$cat=$_POST['cbocategory'];	
		$pd_name=$_POST['txtPdName'];
		$pd_code=$_POST['txtPdCode'];
		
		$selected_date=$_POST['txtDate'];
		$selected_date=strtotime( $selected_date );
		$mysqldate = date( 'Y-m-d', $selected_date );
$inDate=$mysqldate;
		$selected_date=$_POST['txtExDate'];
		$selected_date=strtotime( $selected_date );
		$mysqldate = date( 'Y-m-d', $selected_date );
$expireDate=$mysqldate;
			
			$sql="INSERT INTO tbl_products (cat_id,pd_code,pd_name,buying_price,selling_price, pd_des, date, expire_date)
			VALUES(
				'$_POST[cbocategory]',
				'$_POST[txtPdCode]',
				'$_POST[txtPdName]',
				'$_POST[txtBuying]',
				'$_POST[txtSell]',
				'$_POST[txtDes]',
				'$inDate',
				'$expireDate')";
		
			$result = mysql_query($sql);
			if($result){
				$sql="INSERT INTO tbl_stocks_available (cat_id,pd_code,pd_name, qty)
				VALUES(
				'$_POST[cbocategory]','$_POST[txtPdCode]','$_POST[txtPdName]',0)";
				
				mysql_query($sql);
			}
			pageRedirect("index.php?page=products&action=products_addnew&","Added successful...!");
	}
	
	if(isset($_GET['action']) && $_GET['action'] == "update"){
		$selected_date=$_POST['txtDate'];
		$selected_date=strtotime( $selected_date );
		$mysqldate = date( 'Y-m-d', $selected_date );
$inDate=$mysqldate;
		$selected_date=$_POST['txtExDate'];
		$selected_date=strtotime( $selected_date );
		$mysqldate = date( 'Y-m-d', $selected_date );
$expireDate=$mysqldate;
		$sql= "UPDATE tbl_products SET
				cat_id = '".$_POST['cbocategory']."',
				pd_code = '".$_POST['txtPdCode']."',
				pd_name = '".$_POST['txtPdName']."',
				buying_price = '".$_POST['txtBuying']."',
				selling_price = '".$_POST['txtSell']."',
				pd_des = '".$_POST['txtDes']."',
				date = '".$inDate."',
				expire_date = '".$expireDate."'
			WHERE pd_id = $_GET[id]";
			
		$result = mysql_query($sql);
		$pd_name = $_GET['name'];
		if($result){
			$sql="UPDATE tbl_stocks_available SET cat_id = '".$_POST['cbocategory']."',pd_code='".$_POST['txtPdCode']."' , pd_name = '".$_POST['txtPdName']."' 
				WHERE pd_name = '$pd_name'";
			
			mysql_query($sql);
			pageRedirect("index.php?page=products&action=products&","Updated successful....!");
		}
	}
	
	if(isset($_GET['action']) && $_GET['action'] == "delete"){
		$sql = "DELETE FROM tbl_products
				WHERE pd_id = $_GET[id]";
				
		$result = mysql_query($sql);
		$pd_name = $_GET['name'];
		if($result){
			$sql="DELETE FROM tbl_stocks_available
				WHERE pd_name = '$pd_name'";
			
			mysql_query($sql);
		}
		pageRedirect("index.php?page=products&action=products&","Deleted successful....!");
	}
?>